package linkTest_1 
{
	import fl.controls.Button;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 連線測試 v1
	 * @author cain in liontravel 2010
	 */
	public class Main extends Sprite 
	{
		public var link_bn      :Button;
		public var outPut_td    :TextArea;
		public var port_td      :TextInput;
		public var server_td    :TextInput;
		public var sent_td      :TextInput;
		public var sent_bn      :Button;
		private var _model:Model;
		private var _config:Config;
		
		public function Main() 
		{ 
			setup();
		}
		private function setup():void
		{
			_model = new Model();
			_config = new Config(_model);
			addEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
		}
		private function onADDED_TO_STAGE(e:Event):void 
		{
			addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			init();
		}
		private function init():void
		{
			
			addEventListeners();
		}
		
		private function addEventListeners():void
		{
			link_bn.addEventListener(MouseEvent.CLICK, onCLICK);
			sent_bn.addEventListener(MouseEvent.CLICK, sent_Data);
			_model.addEventListener(Model.OUTPUT, onOUTPUT);
		}
		
		private function sent_Data(e:MouseEvent):void 
		{
			_config.sentData(sent_td.text);
		}
		
		private function onOUTPUT(e:Event):void 
		{
			outPut_td.text = _model.output;
		}
		
		private function onCLICK(e:MouseEvent):void 
		{
			_config.link(server_td.text, port_td.text);
		}
		private function removeEventListeners():void
		{
			link_bn.removeEventListener(MouseEvent.CLICK, onCLICK);
			sent_bn.removeEventListener(MouseEvent.CLICK, sent_Data);
			_model.removeEventListener(Model.OUTPUT, onOUTPUT);
		}
		private function onREMOVED_FROM_STAGE(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
			removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			removeEventListeners();
		}
	}
	
}